home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Source / Positioning Stuff / SkelPosRect.c < prev    next >
Text File  |  1996-01-17  |  1KB  |  46 lines

  1. /*
  2.  * SkelPositionRect()
  3.  *
  4.  * Position a rectangle relative to reference rectangle so that space above/below
  5.  * and to left/right of rectangle that's moved maintains ratio given by hRatio and
  6.  * vRatio.  This is useful for establishing initial window positions or positioning
  7.  * alerts/dialogs.
  8.  *
  9.  * "inside" isn't quite the right word, since the moved rectangle need not
  10.  * actually be smaller than the reference rectangle.
  11.  *
  12.  * Examples:
  13.  *
  14.  * Center a rectangle inside the reference rectangle:
  15.  *    SkelPositionRect (&ref, &r, FixRatio (1, 2), FixRatio (1, 2));
  16.  *
  17.  * Leave 1/3 of vertical space above positioned rectangle, 2/3 of space below:
  18.  *    SkelPositionRect (&ref, &r, FixRatio (1, 2), FixRatio (1, 3));
  19.  *
  20.  * Algorithm may not work correctly if rects have negative coordinates.
  21.  */
  22.  
  23. # include    <FixMath.h>
  24.  
  25. # include    "TransSkel.h"
  26.  
  27.  
  28. pascal void
  29. SkelPositionRect (Rect *refRect, Rect *r, Fixed hRatio, Fixed vRatio)
  30. {
  31. short    hOff, vOff;
  32.  
  33.     /* align topleft of rects (simplifies calculations) */
  34.  
  35.     OffsetRect (r, refRect->left - r->left, refRect->top - r->top);
  36.  
  37.     /* calculate offsets in each direction for given ratios */
  38.  
  39.     hOff = Fix2Long (FixMul (Long2Fix ((long) (refRect->right - r->right)), hRatio));
  40.     vOff = Fix2Long (FixMul (Long2Fix ((long) (refRect->bottom - r->bottom)), vRatio));
  41.  
  42.     /* move rect by appropriate amount */
  43.  
  44.     OffsetRect (r, hOff, vOff);
  45. }
  46.